﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Zheng;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        RHandler rh;
        public Form1()
        {
            InitializeComponent();
            Console.SetOut(new TextBoxWriter(textBox1));
            rh = new RHandler();
        }
        string era5FilePath, modCalFilePath, modProFilePath, modValFilePath;


        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.FormClosing += new FormClosingEventHandler(f2Closing);
            f2.Show();
            this.Enabled = false;
        }

        void f2Closing(object sender, FormClosingEventArgs e)
        {
            this.Enabled = true;
        }
        private void button5_Click(object sender, EventArgs e)
        {
            
            if (era5FilePath != "" && modValFilePath != "" && modCalFilePath != "" && modProFilePath != "")
            {
                this.Enabled = false;
                if (!rh.Started())
                    rh.EngineStart();
                rh.qdm(era5FilePath, modValFilePath, modCalFilePath, modProFilePath);
                this.Enabled = true;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                era5FilePath = openFileDialog1.FileName;
                textBox3.Text = Path.GetFileName(openFileDialog1.FileName);
            }
        }

        //private void button6_Click(object sender, EventArgs e)
        //{
        //    this.Enabled = false;
        //    if (!rh.Started())
        //        rh.EngineStart();
        //    rh.HelloTest();
        //    this.Enabled = true;
        //}

        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                modCalFilePath = openFileDialog1.FileName;
                textBox4.Text = Path.GetFileName(openFileDialog1.FileName);

                int lastInd = openFileDialog1.FileName.LastIndexOf("calibration");
                if (lastInd >= 0)
                {
                    modValFilePath = openFileDialog1.FileName.Remove(lastInd, 11).Insert(lastInd, "validation");
                    modProFilePath = openFileDialog1.FileName.Remove(lastInd, 11).Insert(lastInd, "projected");
                }
            }
        }

        //private void button4_Click(object sender, EventArgs e)
        //{
        //    openFileDialog1 = new OpenFileDialog();
        //    openFileDialog1.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
        //    if (openFileDialog1.ShowDialog() == DialogResult.OK)
        //    {
        //        modProFilePath = openFileDialog1.FileName;
        //        textBox5.Text = Path.GetFileName(openFileDialog1.FileName);
        //    }
        //}

        public class TextBoxWriter : TextWriter
        {
            private TextBox _output = null;

            public TextBoxWriter(TextBox output)
            {
                _output = output;
            }

            public override void Write(char value)
            {
                base.Write(value);
                _output.AppendText(value.ToString());
            }

            public override Encoding Encoding
            {
                get { return System.Text.Encoding.UTF8; }
            }
        }
    }
}
